#include <iostream>
#include <vector>
#include <queue>
#include <array>


struct Neuron {
	int C, U;
};

/**
 * 
 * 神经网络
 * 
 * https://www.luogu.com.cn/problem/P1038
 */
int main() {
	int n, p;
	std::cin >> n >> p;

	std::vector<Neuron> neurons(n + 1);
	std::vector<bool> isOutput(n+1, true);
	std::vector<int> indegree(n + 1);

	for (int i = 1; i <= n; i++) {
		std::cin >> neurons[i].C >> neurons[i].U;
	}

	std::vector<std::vector<std::array<int, 2>>> graph(n + 1);

	for (int k = 0; k < p; k++) {
		int i, j, W;
		std::cin >> i >> j >> W;

		graph[i].push_back({ j,W });
		indegree[j]++;
		isOutput[i] = false;
	}

	std::queue<int> q;
	for (int i = 1; i <= n; i++) {
		if (indegree[i] == 0) {
			q.push(i);
		}
	}

	while (!q.empty()) {
		const int i = q.front();
		q.pop();

		if (neurons[i].C < 0) {
			continue;
		}

		for (const std::array<int, 2>& pair : graph[i]) {
			int j = pair[0];
			int W = pair[1];


			neurons[j].C += W * neurons[i].C;
			if (--indegree[j] == 0) {
				q.push(j);
				neurons[j].C -= neurons[j].U;
			}
		}

	}

	bool hasOutput = false;
	for (int i = 1; i <= n; i++) {
		if (isOutput[i] && neurons[i].C > 0) {
			std::cout << i << " " << neurons[i].C << "\n";
			hasOutput = true;
		}
	}

	if (!hasOutput) {
		std::cout << "NULL\n";
	}


	return 0;
}
